#include "syscall.h"
#include "print.c"

/***********************************************************
 
    Test 2 : use lock syscall in wrong order
             (acquire, release and destroy lock before
              the lock has been created)

************************************************************/

int main()
{
	int lock;    
	int condition;
	int printMsg[5];

	printString("-------------- Starting Test 2 --------------\n");

	printString("-====-Lock Part-====-\n");
	printf("-> Acquire Lock -100\n", printMsg);
	Acquire(-100);
	printf("-> Acquire Lock 10000\n", printMsg);
	Acquire(10000);
	printf("-> Acquire Lock 0\n", printMsg);
	Acquire(0);

	printf("-> Release Lock -100\n", printMsg);
	Release(-100);
	printf("-> Release Lock 10000\n", printMsg);
	Release(10000);
	printf("-> Release Lock 0\n", printMsg);
	Release(0);

	printf("-> Destroy Lock -100\n", printMsg);
	DestroyLock(-100);
	printf("-> Destroy Lock 10000\n", printMsg);
	DestroyLock(10000);
	printf("-> Destroy Lock 0\n", printMsg);
	DestroyLock(0);

	printf("-> Create Lock\n", printMsg);
	lock = CreateLock();
	printMsg[0] = lock;
	printf("-> Lock %d created\n", printMsg);

	printf("-> Acquire Lock %d\n", printMsg);
	Acquire(lock);
	printf("-> Destroy Lock %d, which will be in use so cannot be destroyed yet\n", printMsg);
	DestroyLock(lock); /*Try to destroy a lock that is in use.*/
	printf("-> Release Lock %d\n", printMsg);
	Release(lock);
	printf("-> Destroy Lock %d\n", printMsg);
	DestroyLock(lock);

	
	printString("-=====-Condition Part-=====-\n");
	printf("-> Destroy Condition -100\n", printMsg);
	DestroyCondition(-100);
	printf("-> Destroy Condition 10000\n", printMsg);
	DestroyCondition(10000);
	printf("-> Destroy Condition 0\n", printMsg);
	DestroyCondition(0);

	printf("-> Wait Condition -100 with Lock %d\n", printMsg);
	Wait(-100, lock);
	printf("-> Signal Condition -100 with Lock %d\n", printMsg);
	Signal(-100, lock);
	printf("-> Broadcast Condition -100 with Lock %d\n", printMsg);
	Broadcast(-100, lock);
	printf("-> Wait Condition 10000 with Lock %d\n", printMsg);
	Wait(10000, lock);
	printf("-> Signal Condition 10000 with Lock %d\n", printMsg);
	Signal(10000, lock);
	printf("-> Broadcast Condition 10000 with Lock %d\n", printMsg);
	Broadcast(10000, lock);
	printf("-> Wait Condition 0 with Lock %d\n", printMsg);
	Wait(0, lock);
	printf("-> Signal Condition 0 with Lock %d\n", printMsg);
	Signal(0, lock);
	printf("-> Broadcast Condition 0 with Lock %d\n", printMsg);
	Broadcast(0, lock);

	printString("-=====-Combination of Lock and Condition-=====-\n");
	printf("-> Create Lock\n", printMsg);
	lock = CreateLock();
	printMsg[0] = lock;
	printf("-> Lock %d created\n", printMsg);
	printf("-> Create Condition\n", printMsg);
	condition = CreateCondition();
	printMsg[0] = condition;
	printf("-> Condition %d created\n", printMsg);

	printf("-> Wait Condition %d with Lock 1\n", printMsg);
	Wait(condition, 1);
	printf("-> Signal Condition %d with Lock 1\n", printMsg);
	Signal(condition, 1);
	printf("-> Broadcast Condition %d with Lock 1\n", printMsg);
	Broadcast(condition, 1);

	printMsg[0] = condition;
	printMsg[1] = lock;
	printf("-> Signal Condition %d with Lock %d\n", printMsg);
	Signal(condition, lock);
	printf("-> Broadcast Condition %d with Lock %d\n", printMsg);
	Broadcast(condition, lock);

	printString("----------------- Test 2 End -----------------\n");

	Exit(0);

}




